package com.usian.sms.rabbitmq;

import com.rabbitmq.client.Channel;
import com.alibaba.fastjson.JSONObject;

import com.usian.model.user.dtos.SecurityCodeDto;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SmsMq {

    @RabbitListener(queues = {"SmsQueue"})
    @RabbitHandler
    public void msg(@Payload String msg, Message message, Channel channel) throws IOException {
//        无论验证码取到还是没取到，都要把它从队列中消费掉
        channel.basicAck(message.getMessageProperties().getContentLength(),true);
        JSONObject jsonObject = JSONObject.parseObject(msg);
        SecurityCodeDto codeDto = JSONObject.toJavaObject(jsonObject, SecurityCodeDto.class);
        System.out.println(codeDto.getRandNumCode());


    }
}
